<?php
namespace App\Service\Admin;

class WhywService0907 extends BaseService
{
    //工时录入业务

    //数据列表
    public static function index($pages, $pas = [])
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh)->where('del', 0);
//        if (isset($pas['role_id']) && $pas['role_id']) {
//            $db = $db->where('role_id', $pas['role_id']);
//        }
        if ($pas['uid'] && $pas['role_id'] > 2) {
            $db = $db->where('staff_id', $pas['uid']);
        }
        if (isset($pas['depart_id']) && $pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $types = self::$whStatuss;
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '-' : $itemOne['title'];
            $whOne = (array) self::dbConn(self::$dbConfigWorkHour)
                ->where('id', $v->wh_id)
                ->first();
            $wh_name = empty($whOne) ? '-' : $whOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)
                ->where('id', $v->task_id)
                ->first();
            $task_name = empty($taskOne) ? '-' : $taskOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '-' : $staffOne['title'];
            $statusOne = (array) self::dbConn(self::$dbYwWhStatus)
                ->where('wh_id', $v->id)
                ->orderBy('id', 'desc')
                ->first();
            $status_id = empty($statusOne) ? '-' : $statusOne['type'];
            $status_name = array_key_exists($status_id, $types) ? $types[$status_id] : '-';
            $delay_name = floor($v->delay / 60) . '小时' . ($v->delay % 60) . '分钟';
            $arys[] = [
                'id' => $v->id,
                'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                'wh_id' => $v->wh_id, 'wh_name' => $wh_name,
                'starthour' => $v->starthour, 'endhour' => $v->endhour,
                'delay' => $v->delay, 'delay_name' => $delay_name,
                'staff' => $v->staff_id, 'staff_name' => $staff_name,
                'status_id' => $status_id, 'status_name' => $status_name,
                'lasttime' => $v->uptime,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //详情
    public static function getOne($id)
    {
        $arys['data'] = (array) self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->first();
        $itemOnes = self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->groupBy('config_whid')
            ->get();
        $whList = [];
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
                    ->where('id', $v->config_whid)
                    ->first();
                $title = empty($configOne) ? '' : $configOne['title'];
                $subOnes = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('config_whid', $v->config_whid)
                    ->get();
                $subList = [];
                if (count($subOnes)) {
                    foreach ($subOnes as $vv) {
                        $subList[] = [
                            'id' => $vv->id, 'title' => $vv->title,
                            'start' => $vv->starthour, 'end' => $vv->endhour,
                            'delay' => $vv->delay, 'config_whid' => $v->config_whid,
                        ];
                    }
                }
                $whList[] = [
                    'id' => $v->config_whid, 'title' => $title,
                    'config_whid' => $v->config_whid, 'child' => $subList,
                ];
            }
        }
        $arys['data']['whList'] = $whList;
        //项目
        $arys['item'] = self::dbConn(self::$dbConfigItem)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        //任务
        $arys['task'] = self::dbConn(self::$dbYwTask)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        //工时配置
        $arys['wh'] = self::dbConn(self::$dbConfigWorkHour)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        //时间
        for ($i = 8; $i <= 12; $i ++) {
            if ($i == 12) {
                $arys['hours'][] = '12:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $arys['hours'][] = $ii.':'.$jj;
                }
            }
        }
        for ($i = 13; $i <= 17; $i ++) {
            if ($i == 17) {
                $arys['hours'][] = '17:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $arys['hours'][] = $ii.':'.$jj;
                }
            }
        }
        return self::toback(0, '', $arys);
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //数据列表
    public static function authIndex($pages, $pas = [])
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh)->where('del', 0);
//        if (isset($pas['role_id']) && $pas['role_id']) {
//            $db = $db->where('role_id', $pas['role_id']);
//        }
//        if (isset($pas['depart_id']) && $pas['depart_id']) {
//            $db = $db->where('depart_id', $pas['depart_id']);
//        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $types = self::$whStatuss;
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '-' : $itemOne['title'];
            $whOne = (array) self::dbConn(self::$dbConfigWorkHour)
                ->where('id', $v->wh_id)
                ->first();
            $wh_name = empty($whOne) ? '-' : $whOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)
                ->where('id', $v->task_id)
                ->first();
            $task_name = empty($taskOne) ? '-' : $taskOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '-' : $staffOne['title'];
            $statusOne = (array) self::dbConn(self::$dbYwWhStatus)
                ->where('wh_id', $v->id)
                ->orderBy('id', 'desc')
                ->first();
            $status_id = empty($statusOne) ? '-' : $statusOne['type'];
            $status_name = array_key_exists($status_id, $types) ? $types[$status_id] : '-';
//            $delay_name = floor($v->delay / 60) . '小时' . ($v->delay % 60) . '分钟';
            $arys[] = [
                'id' => $v->id,
                'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                'wh_id' => $v->wh_id, 'wh_name' => $wh_name,
                'starthour' => $v->starthour, 'endhour' => $v->endhour,
//                'delay' => $v->delay, 'delay_name' => $delay_name,
                'staff' => $v->staff_id, 'staff_name' => $staff_name,
                'status_id' => $status_id, 'status_name' => $status_name,
                'lasttime' => $v->uptime,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //审核通过
    public static function authOk($id)
    {
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['status' => 4]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        $res = self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'type' => 4, 'addtime' => toDate(),
        ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //审核拒绝
    public static function authNo($id, $remark)
    {
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['status' => 3]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        $res = self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'type' => 3, 'remark' => $remark, 'addtime' => toDate(),
        ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}